package com.atguigu;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.ByteArrayInputStream;
import java.io.File;

public class FileTest {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        //EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        String accessKeyId="LTAI5tQJiyDtTLmKTLY5oXgS";
        String accessKeySecret="sfYoEyu0wsm6YsyNjaXVOcAkyCYGB1";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "yygh-user-du";
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "2024/6/25/result.jpg";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);


            String filePath = "E:\\桌面文件\\10.jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
        ossClient.putObject(putObjectRequest);


        ossClient.shutdown();

        String url="https://%s.%s/%s";
        String endpoint2="oss-cn-hangzhou.aliyuncs.com";
        url=String.format(url, bucketName,endpoint2,objectName);
        System.out.println("url = " + url);

    }
}
